package com.ct.gd.passport.domain;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import com.ct.gd.passport.common.connect.CtServer;
import com.ct.gd.passport.common.constants.Protocol;
import com.ct.gd.passport.util.TelephonyState;
import com.fuckhtc.gson.Gson;
import com.fuckhtc.gson.annotations.SerializedName;
import com.fuckhtc.gson.reflect.TypeToken;
import com.up91.common.android.connect.Params;
import com.up91.common.android.exception.NullDataException;
import com.up91.common.android.helper.L;

public class PassportAppInfo implements Parcelable {

	@SerializedName("id")
	private String id;

	@SerializedName("serialNumber")
	private String serialNumber;
	
	@SerializedName("addtime")
	private String addtime;

	@SerializedName("name")
	private String name;

	@SerializedName("submitDate")
	private String date;

	@SerializedName("goArea")
	private String goArea;

	@SerializedName("serial")
	private String serial;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getGoArea() {
		return goArea;
	}

	public void setGoArea(String goArea) {
		this.goArea = goArea;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public static List<PassportAppInfo> load() {
		Params params = new Params();
		params.put(Protocol.Fields.IMEI, TelephonyState.getInstance().getIMEI());
//		params.put(Protocol.Fields.USER_PHONE, TelephonyState.getInstance().getPhoneNumber());
		String json = CtServer.getServer().doGet(
				Protocol.Commands.PASSPORT_HISTORY, params);
		L.i("load app info list:", json);
		try {
			JSONObject jObj = new JSONObject(json);
			TypeToken<List<PassportAppInfo>> typeToken = new TypeToken<List<PassportAppInfo>>() {
			};
			return new Gson().fromJson(jObj.getString(Protocol.Fields.DATA),typeToken.getType());
		} catch (JSONException e) {
			e.printStackTrace();
			throw new NullDataException();
		}catch (Exception e) {
			e.printStackTrace();
			throw new NullDataException();
		}

//		 List<PassportAppInfo> infos = new ArrayList<PassportAppInfo>();
//		 PassportAppInfo info = null;
//		 for(int i=1;i<10;i++){
//		 info = new PassportAppInfo();
//		 info.setId(i);
//		 info.setDate("2012-06-0"+i);
//		 info.setGoArea("香港");
//		 info.setName("第"+i+"位测试人");
//		 infos.add(info);
//		 }
//		 return infos;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(id);
		dest.writeString(serialNumber);
		dest.writeString(name);
		dest.writeString(date);
		dest.writeString(goArea);
		dest.writeString(serial);
	}

	public static final Parcelable.Creator<PassportAppInfo> CREATOR = new Creator<PassportAppInfo>() {

		@Override
		public PassportAppInfo[] newArray(int size) {
			return new PassportAppInfo[size];
		}

		@Override
		public PassportAppInfo createFromParcel(Parcel src) {
			PassportAppInfo mPassportAppInfo = new PassportAppInfo();
			mPassportAppInfo.id = src.readString();
			mPassportAppInfo.serialNumber = src.readString();
			mPassportAppInfo.name = src.readString();
			mPassportAppInfo.date = src.readString();
			mPassportAppInfo.goArea = src.readString();
			mPassportAppInfo.serial = src.readString();
			return mPassportAppInfo;
		}
	};

}
